import {Class, Exam, Grade, User} from "../types/mytypes";
//封装localStorage
export const UserStore = {
  setUser: ( data: User): void => {
    localStorage.setItem('user', JSON.stringify(data));
  },
  getUser: (): User => {
    let res = localStorage.getItem('user');
    if (res !== null) {
      return JSON.parse(res);
    } else {
      return {} as User;
    }
  },
};

export const PageStore = {
  setNowPage:(nowPageName:string):void=>{
    sessionStorage.setItem('nowpage', nowPageName);
  },
  getNowPage:():string|undefined=>{
    let res = sessionStorage.getItem('nowpage');
    if(res == null){
      return undefined;
    }
    return res;
  },
  setNowExam:(exam:Exam):void=>{
    sessionStorage.setItem('nowexam', JSON.stringify(exam));
  },
  getNowExam:():Exam=>{
    let res = sessionStorage.getItem('nowexam');
    if(res == null){
      return {} as Exam;
    }
    return JSON.parse(res);
  },
  setNowClass:(obj:Class):void=>{
    sessionStorage.setItem('nowclass', JSON.stringify(obj));
  },
  getNowClass:():Class=>{
    let res = sessionStorage.getItem('nowclass');
    if(res == null){
      return {} as Class;
    }
    return JSON.parse(res);
  },
  setNowGrade:(obj:Grade):void=>{
    sessionStorage.setItem('nowgrade', JSON.stringify(obj));
  },
  getNowGrade:():Grade|undefined=>{
    let res = sessionStorage.getItem('nowgrade');
    if(res == null){
      return undefined;
    }
    return JSON.parse(res);
  },
  setTempDetermineList:(obj:boolean[],gradeId:number):void=>{
    localStorage.setItem('tempdetermine'+gradeId,JSON.stringify(obj))
  },
  getTempDetermineList:(gradeId:number):boolean[]|undefined=>{
    let item = localStorage.getItem('tempdetermine'+gradeId);
    if(item == null) return undefined;
    return JSON.parse(item);
  },
  setNowIndex:(index:string)=>{
    sessionStorage.setItem("now_index",index);
  },
  getNowIndex:():string =>{
    let s = sessionStorage.getItem("now_index");
    if(s == undefined){
      return "0"
    }else{
      return s;
    }
  }
}